package com.iseastar.guojiang.team.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.model.CourierTeamMonthBean;
import com.iseastar.guojiang.team.TeamCenterZoneListActivity;
import com.shouhuobao.station.R;
import com.shouhuobao.take.weight.CircularImage;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.GlideUtil;
import droid.frame.utils.lang.Str;

/**
 * @author 李澄锋<br>
 * 2018/6/19
 */
public class CreatingTeamZoneListAdapter extends BaseAdapterExt<CourierTeamMonthBean> {

    public CreatingTeamZoneListAdapter(ArrayList<CourierTeamMonthBean> items, Activity context) {
        super(items, context);
        setEmptyView(R.layout.courier_team_center_list_empty);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder viewHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_courier_team_zone_creating_list, parent, false);
            viewHolder.teamNameTV = convertView.findViewById(R.id.team_name_tv);
            viewHolder.teamMemberTipTV = convertView.findViewById(R.id.team_member_tip_tv);
            viewHolder.joinTeamTV = convertView.findViewById(R.id.join_team_tv);
            viewHolder.teamLeaderHeaderIV = convertView.findViewById(R.id.team_leader_header_iv);
            viewHolder.teamLeaderInfoTV = convertView.findViewById(R.id.team_leader_info_tv);
            viewHolder.teamDayTV = convertView.findViewById(R.id.team_day_tv);
            viewHolder.teamHourTV = convertView.findViewById(R.id.team_hour_tv);
            viewHolder.lookMemberTeamTV = convertView.findViewById(R.id.look_member_team_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final CourierTeamMonthBean item = getItem(position);
        viewHolder.teamNameTV.setText(item.getTeamName());
        viewHolder.teamMemberTipTV.setText("再有" + item.getOtherMinCourierNum() + "人组队成功");
        viewHolder.lookMemberTeamTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogMgr.courierTeamDetailsDialog(context, item.getImagePath(), item.getNickName(), item.getName(), item.getTeamSlogan(), item.getTeamIntroduce());
            }
        });
        GlideUtil.loadImage(context, item.getImagePath(), R.drawable.setting_head_hover_default, viewHolder.teamLeaderHeaderIV);
        viewHolder.teamLeaderHeaderIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogMgr.courierTeamDetailsDialog(context, item.getImagePath(), item.getNickName(), item.getName(), item.getTeamSlogan(), item.getTeamIntroduce());
            }
        });
        if (Str.isEmpty(item.getName())) {
            viewHolder.teamLeaderInfoTV.setText(item.getNickName() + "（）");
        } else {
            viewHolder.teamLeaderInfoTV.setText(item.getNickName() + "（" + item.getName().substring(0, 1) + "师傅）");
        }
        if (item.getJoinStatus() == 2) {
            viewHolder.joinTeamTV.setBackgroundResource(R.drawable.app_courier_c7c7c7_3_bg);
        } else {
            viewHolder.joinTeamTV.setBackgroundResource(R.drawable.same_city_complete_the_task_6783d8_bg);
        }
        //点击申请加入战队
        viewHolder.joinTeamTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1:审核中，2：已加入，3：未加入
                if (item.getJoinStatus() == 1) {
                    if (item.getJoinTeamId() == item.getTeamId()) {
                        ((TeamCenterZoneListActivity) context).showToast("您已经提交过加入申请了哦！");
                    } else {
                        DialogMgr.courierDuplicateClaimDialog((TeamCenterZoneListActivity) context, item.getJoinTeamName(), item.getTeamId());
                    }
                } else if (item.getJoinStatus() == 3) {
                    DialogMgr.courierNoExitForTwoMonthsDialog((TeamCenterZoneListActivity) context, item.getTeamId());
                }
            }
        });
        long diffTime = item.getTeamCancelTime() - System.currentTimeMillis();
        if (diffTime > 0) {
            String[] diffs = AppLogic.timeDiffDHMSStr(diffTime);
            if (diffs.length == 3) {
                viewHolder.teamDayTV.setText("0");
                viewHolder.teamHourTV.setText(diffs[0]);
            } else if (diffs.length == 4) {
                viewHolder.teamDayTV.setText(diffs[0]);
                viewHolder.teamHourTV.setText(diffs[1]);
            } else {
                viewHolder.teamDayTV.setText("00");
                viewHolder.teamHourTV.setText("00");
            }
        } else {
            viewHolder.teamDayTV.setText("00");
            viewHolder.teamHourTV.setText("00");
        }
        return convertView;
    }

    private class ViewHolder {
        TextView teamNameTV;//战队名称
        TextView teamMemberTipTV;//战队满员提示
        TextView joinTeamTV;//加入战队申请
        CircularImage teamLeaderHeaderIV;//队长头像
        TextView teamLeaderInfoTV;//队长信息
        TextView teamDayTV;//组队剩余天数
        TextView teamHourTV;//组队剩余小时
        TextView lookMemberTeamTV;//查看战队介绍
    }
}
